
package commonpk;

import java.sql.*;


public class DBOperation {
    
    //*******************************************************************************
    //连接参数
    private String               drive = "com.hxtt.sql.access.AccessDriver";    //驱动程序
    private String               url = "jdbc:Access:///GuessFlagDB.accdb";      //数据库地址
    private String               user = "";                                     //用户名
    private String               password = "";                                 //密码
    
    //有关成员
    private Connection           conn;         //连接
    private Statement            stmt;         //陈述
    private PreparedStatement    preStmt;      //预陈述
    private ResultSet            rs;           //结果集
    
    //*****************************************************************************
    /* Getters and Setters */

    public ResultSet getRs() {
        return rs;
    }
    
    //*****************************************************************************
    
    // Use it for drive DB
    public void DBDrive(){
        
        try{
            Class.forName(this.drive);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        
    }
    
    // Use it for connecting DB
    public void DBConnect(){
        DBDrive();
        try{
            conn = DriverManager.getConnection(url, user, password);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            
        }
    }
    
    // Use it for quary DB
    public void DBQuary(String sql){
        DBConnect();
        try{
             this.rs = stmt.executeQuery(sql);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            
        }
    }
    
    /*
    public boolean containsByName(String tableName, String userName){
        
    }
    */
    
    public void DBUpdate(String sql){
        DBConnect();
        try{
            this.stmt.executeUpdate(sql);
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
